package com.wuyan.locationdog.view

import android.content.Context
import android.view.Gravity
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.arcsoft.commonbase.ext.getLifecycleOwner
import com.luozm.captcha.Captcha
import com.luozm.captcha.Captcha.CaptchaListener
import com.wuyan.locationdog.R
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import razerdp.basepopup.BasePopupWindow


class CaptchaPopup(context: Context,var sureCallback: () -> Unit) : BasePopupWindow(context) {



    init {
        contentView = createPopupById(R.layout.pop_captcha)
        popupGravity = Gravity.CENTER
        setBackPressEnable(false)
        setOutSideDismiss(false)
        isOutSideTouchable = false
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        findViewById<Captcha>(R.id.captCha).apply {
            setCaptchaListener(object :CaptchaListener{
                override fun onAccess(time: Long): String {
                    context.getLifecycleOwner()?.lifecycleScope?.launch {
                        delay(1000)
                        sureCallback.invoke()
                        dismiss()
                    }
                    return ""
                }

                override fun onFailed(failCount: Int): String {
                    return ""
                }

                override fun onMaxFailed(): String {
                    return ""
                }
            })
        }

    }
}
